$().ready(function(){

    $("#submit").click(function(){
        $("#interactLogin").submit();
        return false;
    })
    
    $("body").bind('keyup', function(event){
        if (event.keyCode == 13) {
            $("#interactLogin").submit();
        }
    });
    
    jQuery.validator.addMethod("stringCheck", function(value, element){
        return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
    }, "只能包括中文字、英文字母、数字和下划线");
    
    // 中文字两个字节       
    jQuery.validator.addMethod("byteRangeLength", function(value, element, param){
        var length = value.length;
        for (var i = 0; i < value.length; i++) {
            if (value.charCodeAt(i) > 127) {
                length++;
            }
        }
        return this.optional(element) ||
        (length >= param[0] && length <= param[1]);
    }, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)");
    
    $("#interactLogin").validate({
        submitHandler: function(form){
            $(form).ajaxSubmit({
                dataType: "json",
                success: function(data){
                    if (!data.success) {
                        $("#errormsg").fadeIn("slow").text(data.message);
                    }
                    else {
                        document.location.href = data.jump;
                    }
                    
                }
            });
        },
        rules: {
            uname: {
                required: true,
                byteRangeLength: [2, 20],
                stringCheck: true
            },
            upass: {
                required: true,
                minlength: 6,
                maxlength: 20
            }
        },
        messages: {
            uname: {
                required: "请填写用户名",
                byteRangeLength: "用户名必须在3-15个字符之间(一个中文字算2个字符)"
            },
            upass: {
                required: "请输入您的密码",
                minlength: "密码长度不能小于6个字符",
                maxlength: "密码长度不能大于20个字符"
            }
        }
    });
    
})
